package com.hq.fxh.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hq.fxh.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import jp.wasabeef.glide.transformations.BlurTransformation;

import static android.R.attr.width;
import static cn.jpush.android.api.JPushInterface.a.g;
import static cn.jpush.android.api.JPushInterface.a.i;
import static cn.jpush.android.api.JPushInterface.a.n;
import static cn.jpush.android.api.JPushInterface.a.v;
import static com.hq.fxh.imageLoader.video.VideoPicker.TAG;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/14
 * @Email xlwenhao1858@sina.com
 */


public class Tool {
    /*判断按钮是否连击
    * */
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    public static void setImageBG(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).bitmapTransform(new BlurTransformation(context, 22)).into(imageView);

    }


    public static String getplateUrl(int uid, int postid, int type) {
     /*   param name="type">1新闻 2帖子 3视频*/

        String url = "http://news.ledianduo.cn/bbs/html/postinfo.html?uid=" + uid + "&postid=" + postid + "&type=" + type;

        if (uid == 0) {
            url = "http://news.ledianduo.cn/bbs/html/postinfo.html?postid=" + postid + "&type=" + type;
            LogUtils.e("用户没有登录");

        }
        return url;
    }


    public static String getVideoUrl(int uid, String postid, int type) {
     /*   param name="type">1新闻 2帖子 3视频*/

        String url = "http://news.ledianduo.cn/bbs//html/vedioinfo.html?uid" + uid + "&postid=" + postid + "&type=3";

        if (uid == 0) {
            url = "http://news.ledianduo.cn/bbs//html/vedioinfo.html?uid" + uid + "&postid=" + postid + "&type=3";
            LogUtils.e("用户没有登录");
        }
        return url;
    }

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    public static void setLayoutWH(LinearLayout ss, int width, int height) {
        ss.setLayoutParams(new LinearLayout.LayoutParams(width, height));
    }

    public static void setViewWH(View image, int width, int height) {
        if (image == null) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
        if (layoutParams == null) {
            return;
        }
        if (height == 0) {
            height = layoutParams.height;
        }
        layoutParams.height = height;


        if (width == 0) {
            width = layoutParams.width;
        }
        layoutParams.width = width;

        image.setLayoutParams(layoutParams);

    }

    /*设置屏幕透明度*/
    public static void setAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().setAttributes(lp);
    }

    // 判断edit是否为空
    public static boolean editIsEmpty(EditText ed) {
        boolean empty = TextUtils.isEmpty(ed.getText());
        if (empty) {
            return true;
        }
        String s = ed.getText().toString();
        Log.e("字符串", s);
        for (char c : s.toCharArray()) {
            if (c != 32) {

                return false;
            }
        }
        return true;
    }

    public static int getIntFromString(String str) {
        int a = 0;
        try {
            a = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            LogUtils.e("String 转 int 错误=" + str);
        }
        return a;
    }

    public static float getFloatFromString(String str) {
        float a = 0;
        try {
            a = Float.parseFloat(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            LogUtils.e("String 转 float 错误=" + str);

        }
        return a;
    }


    public static void setEditorChange(final EditText editText, final ImageView imageView, final int pic, final int pic_to) {

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    imageView.setImageResource(pic_to);
                } else {
                    imageView.setImageResource(pic);
                }

            }
        });
    }


    // 给textview赋值
    public static void setStringToText(TextView text, String stringContent) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("string为空");
            } else {
                if (text != null) {
                    text.setText(stringContent);
                } else {
                    LogUtils.e(stringContent + "VIew为空");
                    text.setText("");

                }
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    public static void setIntToText(TextView text, int item_count, String name) {
        try {
            if (item_count <= 0) {
                LogUtils.e(name + "  " + item_count + "小于0");
                text.setText(item_count + "");

            } else {
                text.setText(item_count + "");
            }
        } catch (Exception e) {
            LogUtils.e(item_count + "赋值报错");

        }
    }


    public static void setIntTiSiToText(TextView text, int item_count, String name) {
        try {
            if (item_count <= 0) {
                text.setVisibility(View.GONE);

            } else if (item_count > 99) {
                text.setVisibility(View.VISIBLE);
                text.setText("99+");
            } else {
                text.setVisibility(View.VISIBLE);
                text.setText(item_count + "");
            }
        } catch (Exception e) {
            LogUtils.e(item_count + "赋值报错");

        }
    }

    public static void setStringToEdit(EditText text, String stringContent) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                text.setText("");
            } else {
                text.setText(stringContent);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    //给imageview赋值
    public static void setUrlToImage(Context context, String stringContent, ImageView imageView) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("ImageView为空");
            } else {
                GlideUtils.loadImageView(context, stringContent, imageView);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    public static void setUrlToHeadImage(Context context, String stringContent, ImageView imageView) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("ImageView为空");
            } else {
                GlideUtils.loadImageView(context, stringContent, imageView);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }


    public static void setUrlToImage1(Context context, String stringContent, ImageView imageView) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("ImageView为空");
            } else {
                GlideUtils.loadImageViewCen(context, stringContent, imageView);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    static int totalHeight = 0;


    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }

          /*  if (v != null) {
                totalHeight += v.getHeight();
            }
*/
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1))
                + 10;
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }





/*    public static void getHeight(final View textView, final MyCallBack myCallBack) {
        ViewTreeObserver observer = textView.getViewTreeObserver();

        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);//避免重复监听
                LogUtils.e("进入回调1");
                int height = textView.getMeasuredHeight();//获文本高度
                //获取高度后要进行的操作就在这里执行，在外面可能onGlobalLayout还没有执行而获取不到height

                ViewGroup.LayoutParams lp = textView.getLayoutParams();

                lp.width = width;
                lp.height = height;//lp.height=LayoutParams.WRAP_CONTENT;
                textView.setLayoutParams(lp);
                 myCallBack.callBack(height);

            }
        });


    }*/


    public static void openActivity(Context context, Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(context, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        context.startActivity(intent);
    }

    public static Bitmap getbitmapFromUrl(String imageUri) {
        Log.v(TAG, "getbitmap:" + imageUri);
        // 显示网络上的图片
        Bitmap bitmap = null;
        try {
            URL myFileUrl = new URL(imageUri);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
            LogUtils.e("转换成功");
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            LogUtils.e("转换错误1");
            bitmap = null;
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e("转换错误2");
            Log.v(TAG, "getbitmap bmp fail---");
            bitmap = null;
        }
        return bitmap;
    }

    public interface MyCallBack {
        void callBack(int height);
    }


}
